﻿using MathNet.Numerics.Optimization.LineSearch;
using MediatR;
using NPOI.SS.UserModel;
using ProductionManagement.Application.Command.ProOrderCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Dtos;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.ProOrderCommandHandlers
{
    public class SelectProOrderCommandHandlers : IRequestHandler<SelectProOrderCommand, PageDTO<SelectOrderDTO>>
    {

        private readonly IProOrderRepository _proOrder;
        private readonly IProOrderDetailRepository _proOrderDetail;
        private readonly IProductionPlanRepository _productionPlan;
        private readonly IProductionProductRepository productionProducts;
        private readonly ISqlSugarClient db;

        public SelectProOrderCommandHandlers(IProOrderRepository proOrder, IProOrderDetailRepository proOrderDetail, IProductionPlanRepository productionPlan, ISqlSugarClient db, IProductionProductRepository productionProducts)
        {
            _proOrder = proOrder;
            _proOrderDetail = proOrderDetail;
            _productionPlan = productionPlan;
            this.productionProducts = productionProducts;
            this.db = db;

        }

        public async Task<PageDTO<SelectOrderDTO>> Handle(SelectProOrderCommand request, CancellationToken cancellationToken)
        {
            var orders = (await _proOrder.GetAll()).ToList();
            var details = (from a in await _proOrderDetail.GetAll() select a).ToList();
            var plans = (await _productionPlan.GetAll()).ToList();
            var products = (await productionProducts.GetAll()).ToList();

 
           var list= from b in details
                     join a in orders on b.POId equals a.POrderNumber
                     join c in plans on b.ProductionPlansId equals c.ProductionPlansId
            join d in products on c.PPDocumentNumber equals d.PPDocumentNumber
            where a.IsDel==false
            group d by new { a.POId, a.PRemark, a.POrderNumber, a.POrderTopic, a.PPlanName, a.PDocumentDate,a.PDocumentMakers,a.TotalData} into grouped
            select new SelectOrderDTO
            {
                POId = grouped.Key.POId,
                POrderNumber = grouped.Key.POrderNumber,
                POrderTopic = grouped.Key.POrderTopic,
                PDocumentDate = grouped.Key.PDocumentDate,
                PDocumentMakers = grouped.Key.PDocumentMakers,
                TotalData = grouped.Key.TotalData,
                PPlanName = grouped.Key.PPlanName,
                Pname = string.Join(",", grouped.Select(x => x.PName))
            };

          

            if (!string.IsNullOrEmpty(request.OrderNum))
            {
                list = list.Where(x => x.POrderNumber.Contains(request.OrderNum)).ToList();
            }
            if (!string.IsNullOrEmpty(request.OrderTopic))
            {
                list = list.Where(x => x.POrderTopic.Contains(request.OrderTopic)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProduceName))
            {
                list.Where(x => x.Pname.Contains(request.ProduceName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.PlanName))
            {
                list = list.Where(x => x.PPlanName .Contains(request.PlanName)).ToList();
            }

            int totalcount = list.Count();
            int pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pageSize);

            PageDTO<SelectOrderDTO> query = new PageDTO<SelectOrderDTO>
            {
                Totalcount = totalcount,
                Pagecount = pagecount,
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList()
            };

            return query;
        }
    }
}
